import {
  paysend
} from "../../../http/api"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payTotal: 44.66,
    orderNum: 0,
    id: "",
    orderType: 0,
    price: 0,
    amountPrice:0


  },

  handlePay() {
    const {id,orderNum,amountPrice,orderType} = this.data


    const payParams = {
      amountPrice,
      orderNum,
      // amountPrice: 0.02,
      orderType,
      orderId: id,
      openId: wx.getStorageSync('openid')
    }



    wx.showLoading()
    paysend(payParams).then(res => {
      if (res.data.code === 200) {
        const {
          nonceStr,
          paySign,
          timeStamp
        } = res.data.data
        const package1 = res.data.data.package
        wx.showLoading({
          title: '支付中',
        })
        wx.requestPayment({
          timeStamp,
          nonceStr,
          package: package1,
          signType: 'MD5',
          paySign,
          success(successRes) {
            if (successRes.errMsg === 'requestPayment:ok') {
              
              wx.redirectTo({
                url: `/pages/user/success/success?id=${payParams.orderId}`,
              })
            }
          },
          fail(failRes) {
            console.log(failRes)
            if (failRes.errMsg === "requestPayment:fail cancel") {
              wx.showModal({
                title: '提示',
                content: '未完成支付，可稍后在订单中继续支付',
                showCancel: false,
                complete: (res) => {

                  if (res.confirm) {
                    wx.switchTab({
                      url: '/pages/user/order/order',
                    })
                  }
                }
              })
            }

          },
          complete() {
            wx.hideLoading()
          }
        })

      } else {
        wx.showModal({
          title: '提示',
          content: '调起支付失败',
        })
      }


    }).finally(() => {
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {
      id,
      orderNum,
      amountPrice,
      orderType
    } = options
    this.setData({
      id,
      orderNum,
      amountPrice,
      orderType
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})